﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChungNhanSinhVien.Entities;
using ChungNhanSinhVien.Infrastructures;
using System.Web.Mvc;

namespace ChungNhanSinhVien.Models
{
    public class ChungNhanRepository
    {
        Web_ChungNhanSinhVienEntities data;
        public ChungNhanRepository()
        {
            //if (data == null)
                data = new Web_ChungNhanSinhVienEntities();
        }
        public List<ChungNhan> LayTatCaChungNhan()
        {
            var list = new List<ChungNhan>();
            list = (from m in data.ChungNhans
                        select m).ToList();
            return list;
        }

        public List<SelectListItem> LayChungNhansTheoLoaiCN(int loaicnID)
        {
            var list = (from m in data.ChungNhans
                        where m.LoaiChungNhanID.Value.Equals(loaicnID)
                        select m).ToList()
                            .Select(slt => new SelectListItem()
                            {
                                Text = slt.TenChungNhan,
                                Value = slt.ID.ToString()
                            }).ToList();
            return list;
        }

        public ChungNhan LayChungNhanTheoTenChungNhan(string name)
        {
            var kq = (from m in data.ChungNhans
                      where m.TenChungNhan.Equals(name)
                      select m).FirstOrDefault();
            return kq;
        }

        public int LayIDTheoTen(string name)
        {
            var kq = (from m in data.ChungNhans
                      where m.TenChungNhan.Equals(name)
                      select m.ID).FirstOrDefault();
            return kq;
        }

        public List<ChungNhan> LayChungNhansTheoLoaiCNID(int loaicnID)
        {
            var list = (from m in data.ChungNhans
                        where m.LoaiChungNhanID.Value.Equals(loaicnID)
                        select m).ToList();
            return list;
        }

        public int DemChungNhansTheoLoaiCNID(int loaicnID)
        {
            var count = (from m in data.ChungNhans
                         where m.LoaiChungNhanID.Value.Equals(loaicnID)
                         select m).Count();
            return count;
        }

        public bool TaoMoi(ChungNhan cn)
        {
            if (cn != null)
            {
                var kq = new ChungNhan();
                kq.LoaiChungNhanID = cn.LoaiChungNhanID;
                kq.TenChungNhan = cn.TenChungNhan;
                kq.NoiDungChungNhan = cn.NoiDungChungNhan;
                kq.GhiChu = cn.GhiChu;
                data.AddToChungNhans(kq);
                data.SaveChanges();
                return true;
            }
            return false;
        }

        public bool CapNhat(ChungNhan cn)
        {
            if (cn != null)
            {
                var kq = (from m in data.ChungNhans
                          where m.ID.Equals(cn.ID)
                          select m).FirstOrDefault();
                if (kq != null)
                {
                    kq.TenChungNhan = cn.TenChungNhan;
                    kq.NoiDungChungNhan = cn.NoiDungChungNhan;
                    kq.GhiChu = cn.GhiChu;
                    kq.LoaiChungNhanID = cn.LoaiChungNhanID;
                    data.SaveChanges();
                    return true;
                }
            }
            return false;
        }

        public bool Xoa(int id)
        {            
            if (id != 0)
            {
                try
                {
                    var cn = LayDoiTuongTheoID(id);
                    data.DeleteObject(cn);
                    data.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            return false;
        }

        public ChungNhan LayDoiTuongTheoID(int id)
        {
            var kq = (from m in data.ChungNhans
                      where m.ID.Equals(id)
                      select m).FirstOrDefault();
            return kq;
        }

        public bool KiemTraTonTaiTheoTenVaLoaiCN(string tencn, int loaicnID, int cnID)
        {
            var kq = (from m in data.ChungNhans
                      where m.TenChungNhan.Equals(tencn)
                      && m.LoaiChungNhanID.Value.Equals(loaicnID)
                      && !m.ID.Equals(cnID)
                      select m.ID).FirstOrDefault();
            if (kq != 0)
                return true;
            return false;
        }
    }
}